{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "\n",
    "import vtk\n",
    "\n",
    "import itkwidgets\n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywebrtc as webrtc\n",
    "\n",
    "from reportlab.pdfgen.canvas import Canvas\n",
    "from reportlab.lib.pagesizes import letter, inch\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "soReader = itk.SpatialObjectReader[3].New()\n",
    "soReader.SetFileName(\"Data/MRI-Normals/Normal003-VascularNetwork.tre\")\n",
    "soReader.Update()\n",
    "vess = soReader.GetGroup()\n",
    "\n",
    "vtpFileName = \"Data/MRI-Normals/Normal003-VascularNetwork.vtp\"\n",
    "vtpWriter = ttk.WriteTubesAsPolyData.New()\n",
    "vtpWriter.SetInput(vess)\n",
    "vtpWriter.SetFileName(vtpFileName)\n",
    "vtpWriter.Update()\n",
    "\n",
    "reader = vtk.vtkXMLPolyDataReader()\n",
    "reader.SetFileName(vtpFileName)\n",
    "reader.Update()\n",
    "vessVtp = reader.GetOutput()\n",
    "\n",
    "img = itk.imread(\"Data/MRI-Normals/Normal003-MRA.mha\", itk.F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "43bd7e5eb69f4bc9ab95efd06fe1881a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(camera=array([[ 3.5000000e+02,  1.0000000e+00,  3.5000000e+02],\n",
       "       [ 1.5000000e+02, -1.0000000e+01,…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "vtk_camera = vtk.vtkCamera()\n",
    "vtk_camera.SetPosition([350,  1,  350])\n",
    "vtk_camera.SetFocalPoint([ 150,  -10, -300 ])\n",
    "vtk_camera.SetViewUp([-4.5256451e-01,  1.3861613e-02,  -8.9162391e-01])\n",
    "\n",
    "viewer = view(image=img, geometries=vessVtp, cmap=itkwidgets.cm.bone, gradient_opacity=0.8)\n",
    "viewer.camera=vtk_camera\n",
    "viewer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ed8d5195bfb543b9a825dc1ac41fe2c9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "viewer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_recorder = webrtc.ImageRecorder(stream=viewer, filename='Patient001.jpg', autosave=True)\n",
    "image_recorder.recording=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "canvas = Canvas(\"report.pdf\",pagesize=letter)\n",
    "canvas.drawString(4.25 * inch, 10 * inch, \"Patient001\")\n",
    "canvas.drawImage(\"Patient001.jpg\", 4*inch, 5*inch, width=2*inch, height=2*inch)\n",
    "canvas.showPage()\n",
    "canvas.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
